<template>
  <div>
    <a-modal
      :visible="visible"
      :title='title'
      :maskClosable="false"
      :keyboard="false"
      :confirm-loading="confirmLoading"
      width="650px"
      @cancel="() => { $emit('cancel') }"
      @ok="() => { $emit('create') }"
    >
      <a-form :form="form">

        <a-row :gutter="20">
          <a-col :span="12">
            <a-form-item label="姓名" :colon="false" :label-col="{span: 6}" :wrapper-col="{span: 18}">
              <a-input
                :disabled="inputDisabled"
                v-decorator="[
            'userName',
            { initialValue: '', rules: [{type: 'string', required: true, message: '请输入姓名' }] },
          ]"
                placeholder="请输入姓名"
              >
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="工号" :colon="false" :label-col="{span: 6}" :wrapper-col="{span: 18}" >
              <a-input
                :disabled="inputDisabled"
                v-decorator="[
            'workNo',
            {initialValue: '', rules: [{type: 'string',  required: true, message: '请输入工号' }] },
          ]"
                placeholder="请输入工号"
              >
              </a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="20">
          <a-col :span="12">
            <a-form-item label="电话" :colon="false" :label-col="{span: 6}" :wrapper-col="{span: 18}">
              <a-input
                v-decorator="[
            'telephone',
            {initialValue: '',  rules: [{ type: 'string', required: true, message: '请输入手机号码' },{ pattern: '^1(3|4|5|6|7|8|9)\\d{9}$', message: '请输入正确的手机号'}] },
          ]"
                placeholder="请输入手机号码"
              >
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="车牌" :colon="false" :label-col="{span: 6}" :wrapper-col="{span: 18}">
              <a-input
                v-decorator="[
            'licensePlate',
            { initialValue: '', rules: [{type: 'string',  required: true, message: '请输入车牌' },
            { pattern: '^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$', message: '请使用大写字母输入正确的车牌号'}] },
          ]"
                placeholder="请输入车牌"
              >
              </a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="20">
          <a-col :span="12">
            <a-form-item label="类型" :colon="false" :label-col="{span: 6}" :wrapper-col="{span: 18}" required>
              <a-select
                v-decorator="[
                    'statusId',
                    { initialValue: 60000,rules: [{ required: true, message: '请选择车辆类型' }] },
                  ]"
                placeholder="请选择车辆类型"
              >
                <a-select-option :key="item.id" v-for="item in carstype" :value="item.id">
                  {{item.name}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="名单类型" :colon="false" :label-col="{span: 6}" :wrapper-col="{span: 18}" required>
              <a-select
                v-decorator="[
                    'isblack',
                    { initialValue: 0,rules: [{ required: true, message: '请选择名单类型' }] },
                  ]"
                placeholder="请选择名单类型"
              >
                <a-select-option :key="item.id" v-for="item in listType" :value="item.id">
                  {{item.name}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>

        <!--分开写日期-->
        <!--<a-row :gutter="20">-->
          <!--<a-col :span="12">-->
            <!--<a-form-item label="开始时间" :colon="false" :label-col="{span: 6}" :wrapper-col="{span: 18}">-->
              <!--<a-date-picker format="YYYY-MM-DD HH:mm" :show-time="{ format: 'HH:mm' }"-->
                             <!--v-decorator="['startDate', config]" style="width: 100%;"/>-->
            <!--</a-form-item>-->
          <!--</a-col>-->
          <!--<a-col :span="12">-->
            <!--<a-form-item label="结束时间" :colon="false" :label-col="{span: 6}" :wrapper-col="{span: 18}">-->
              <!--<a-date-picker format="YYYY-MM-DD HH:mm" :show-time="{ format: 'HH:mm' }"-->
                             <!--v-decorator="['endDate', config]"  style="width: 100%;"/>-->
            <!--</a-form-item>-->
          <!--</a-col>-->
        <!--</a-row>-->

        <!--组合日期-->
        <a-row :gutter="20">
          <a-col :span="12">
            <a-form-item label="有效期" :colon="false" :label-col="{span: 6}" :wrapper-col="{span: 18}">
              <a-range-picker @change="modalChangePicker"  :defaultPickerValue="defaultAllDate" v-decorator="['allDate', config2]" format="YYYY-MM-DD HH:mm" :show-time="{ format: 'HH:mm' }"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">

          </a-col>
        </a-row>

      </a-form>
    </a-modal>
  </div>
</template>

<script>
  import moment from "moment";
  import dateUtil from "@/utils/dateUtils";
  import reservationApi from "@/api/reservation";

  let startDated = moment(moment().startOf('day')).format('YYYY-MM-DD HH:mm');

  export default {
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
        default: "新增"
      },
      confirmLoading: {
        type: Boolean,
        default: false
      },
    },
    data() {
      return {
        startDate: "",
        endDate: "",
        inputDisabled: false,
        modalStartDated: "",//弹窗开始日期
        modalEndDated: "",//弹窗结束日期
        statusValue: "员工车辆",  //类型名称
        statusId: 60000,   //类型id
        carstype: [
          // {name: "VIP用户", id: 60000},
          // {name: "员工车辆", id: 60001},
        ],
        config: {
          rules: [{type: 'object', required: true, message: '请选择一个时间'}],
        },
        config2: {
          rules: [{type: 'array', required: true, message: '请选择一个时间区间'}],
        },
        listType: [
          {name: "白名单", id: 0},
          {name: "黑名单", id: 1},
          {name: "灰名单", id: 2},
        ],
        defaultAllDate: [moment( startDated, 'YYYY-MM-DD HH:mm'),moment("2050-12-01 00:00:00", 'YYYY-MM-DD HH:mm')]
      }
    },
    beforeCreate() {
      this.form = this.$form.createForm(this, {name: 'form_in_modal'});

      // [moment("2020-12-01 00:00:00", 'YYYY-MM-DD HH:mm'),moment("2050-12-01 00:00:00", 'YYYY-MM-DD HH:mm')]
    },
    beforeMount(){
      // this.startDate = moment(dateUtil.getFirstDay(), this.dateFormat);
      // this.endDate = moment(dateUtil.getLastDay(), this.dateFormat);
      this.getcarsTypeList();
    },
    methods: {
      // 获取车辆类型
      async getcarsTypeList(){
        let resp = await reservationApi.getVehicleTypeList();
        // console.log(resp);
        if (resp.data.success){
          // console.log(resp.data.result)
          this.carstype = resp.data.result
        } else {
          this.$message.error(resp.data.message);
        }
      },
      // 弹窗选择日期
      modalChangePicker(date, dateString) {
        // console.log(date, dateString);
        this.modalStartDated = dateString[0];
        this.modalEndDated = dateString[1];
      },
      // 重置表单默认值
      initFormValue(){
        this.form.setFieldsValue({
          userName:"",
          workNo:"",
          telephone:"",
          licensePlate:"",
          allDate:[],
          statusId: 60000,
          isblack: 0,
          // startDate:"",
          // endDate:"",
        });
      },
      // 填充弹窗
      stuffDilog(item,disabled){
        console.log(item);
        if (item){
          this.form.setFieldsValue({
            userName: item.userName,
            workNo: item.userNo,
            telephone: item.userTelephone,
            licensePlate: item.chepai,
            statusId: item.vehicleTypeId,  //类型名称
            isblack: Number(item.isblack),
            allDate:[moment(item.startDate, 'YYYY-MM-DD HH:mm'),moment(item.endDate, 'YYYY-MM-DD HH:mm')],
            // startDate:"",
            // endDate:"",
          });
          this.inputDisabled = disabled;
        }
      },
      // 类型下拉菜单
      handleMenuClick(e) {
        // console.log(e);
        this.carstype.filter(el => {
          if (el.id == e.key) {
            this.statusValue = el.name;
            this.statusId = el.id
          }
        });
      },


    }

  }
</script>

<style scoped>

</style>
